package com.lovo.homework.Day;

import java.security.Key;
import java.security.KeyStore;
import java.util.*;

public class Day7_12a {
    //给定["a", "b", "a", "b", "c", "a", "b", "c", "b"]字符串数组，
//使用Map的key来保存数组中字符串元素，value保存该字符串元素的出现次数，
//最后统计出各字符串元素的出现次数。
    public static void main(String[] args) {
        String[] str1 = {"a", "b", "a", "b", "c", "a", "b", "c", "b"};
        //关键list集合1["a", "b", "a", "b", "c", "a", "b", "c", "b"]
        List<String> list1 = Arrays.asList(str1);
        Set<String> set = new HashSet();
        set.addAll(list1);
        //关键list集合2["a","b","c"]
        List<String> list2 = new ArrayList();
        list2.addAll(set);
        //value数组
        int[] values = new int[list2.size()];
        //key值数组
        String[] keys = new String[list2.size()];
        for (int i = 0; i < list2.size(); i++) {
            int time = 0;
            keys[i] = list2.get(i);
            for (int j = 0; j < list1.size(); j++) {
                if (list2.get(i).equals(list1.get(j))) {
                    time++;
                }
            }
            values[i] = time;
        }
        //给map赋值
        Map map = new HashMap();
        for (int k = 0; k < keys.length; k++) {
            map.put(keys[k], values[k]);
        }
        System.out.println(map);


    }
}
